package fgoScript.entity.guda;

import commons.util.GameUtil;
import fgoScript.constant.GameConstant;
import fgoScript.constant.PointInfo;
import fgoScript.entity.CardComparator;
import fgoScript.entity.CommonCard;
import fgoScript.entity.PointColor;
import fgoScript.service.CommonMethods;
import fgoScript.service.EventFactors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class ExpApGudazi extends AbstractApGudazi {
    private static final Logger LOGGER = LogManager.getLogger(ExpApGudazi.class);
	@Override
	public void intoAndSelect(int apNum, int acountNum) throws Exception {
		insertIntoExpRoom(apNum);

	}

	@Override
	public void fightAndStop(boolean rebootFlag, int apNum, int account) throws Exception {
		fightAndBackForExp(rebootFlag);

	}

	@Override
	public Point getSuppotServant() {
		return PointInfo.getInstance().getpServantCaster();
	}

	@Override
	public void fightOverMethod() {
	}


	public void insertIntoExpRoom(int apNum) throws Exception {

		GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestDown(),true,true);

		GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

		GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestTop(),true,true);

		GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 进入训练场
        // 周回进去

		GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getP_WEEK_ENTRANCE(),true,true);

		GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
        
        GameUtil.delay(GameConstant.DELAY*5);
        

		GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestTop(),true,true);

		GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

		GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestDown(),true,true);

		GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 拖拽画面
        Point start = PointInfo.getInstance().getpDailySliceStrat();
        Point end = PointInfo.getInstance().getpDailySliceEnd();
        moveBySteps(start, end);

        // 点击日常
        Point p6 = PointInfo.getInstance().getpDailyEntrance();

		GameUtil.getMymouse().mouseMoveByPoint(p6,true,true);

		GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
        

		GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestTop(),true,true);

		GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

		GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestDown(),true,true);

		GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 拉滚动条至中间
        Point p7 = PointInfo.getInstance().getpExpAll();

		GameUtil.getMymouse().mouseMoveByPoint(p7,true,true);

		GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 点击ap本
        Point p8 = PointInfo.getInstance().getExpRoom(apNum);

		GameUtil.getMymouse().mouseMoveByPoint(p8,true,true);

		GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
    }
	private void fightAndBackForExp(boolean rebootFlag) throws Exception {
		int THRESHOLD = GameConstant.THRESHOLD;
		// 1回合数字高亮点
		Point ps1 = new Point(903, 79);
		Point ps2 = new Point(903, 69);
		Point ps3 = new Point(904, 74);
		List<Point> pointList = new ArrayList<>();
		pointList.add(ps1);
		pointList.add(ps2);
		pointList.add(ps3);
		int eveValue;
		int battleRounds = 0;
		int MaxRounds = 20;
		// 第一回合
		LOGGER.info("准备战斗，等待。。。。");
		CommonMethods.waitToAttack(null,LOGGER);
		LOGGER.info("第一回合开始：");
		boolean[] hases = {false,false};
		eveValue = GameUtil.getColorEveValue(pointList);
		if (rebootFlag) {
			DEFAULT_SKILL_COLORS = EventFactors.getDefaultSkillColorsFromFile();
		}else {
			DEFAULT_SKILL_COLORS = EventFactors.getSkillColors();
			EventFactors.writeDefaultSkillColors(DEFAULT_SKILL_COLORS);
		}
		while (eveValue > THRESHOLD && battleRounds < MaxRounds) {
			battleRounds++;
			/*
			  连续使用技能组
			 */
			giveServantSkills(EventFactors.getExpPreSkills(DEFAULT_SKILL_COLORS));
			CommonMethods.waitToAttack(null,LOGGER);
			// 副宝具平a
			CommonMethods.attackBAAForEvent(false, hases, LOGGER);
			// 等待
			CommonMethods.waitToAttack("1", LOGGER);
			eveValue = GameUtil.getColorEveValue(pointList);
		}
		// 第二回合
		// 2回合数字高亮点
		Point ps5 = new Point(906, 70);// 颜色：209;209;209 Color c = new Color(209, 209, 209);
		Point ps6 = new Point(900, 66);// 颜色：255;255;255 Color c = new Color(255, 255, 255);
		Point ps7 = new Point(899, 81);// 颜色：247;247;247 Color c = new Color(247, 247, 247);

		pointList = new ArrayList<>();
		pointList.add(ps5);
		pointList.add(ps6);
		pointList.add(ps7);
		CommonMethods.waitToAttack(null,LOGGER);
		LOGGER.info("第二回合开始：");
		eveValue = GameUtil.getColorEveValue(pointList);
		
		PointColor pc;
		while (eveValue > THRESHOLD&& battleRounds < MaxRounds) {
			battleRounds++;
			/*
			  连续使用技能组
			 */
			giveServantSkills(EventFactors.getExpPreSkills(DEFAULT_SKILL_COLORS));
			pc = CommonMethods.waitToAttack(null,LOGGER);
			if ("attack".equals(pc.getName())) {
				CommonMethods.attackBAAForEvent(true, hases,LOGGER);
			}else {
				LOGGER.info("羁绊了，跳出来");
				break;
			}
			// 等待
			CommonMethods.waitToAttack("3",LOGGER);
			eveValue = GameUtil.getColorEveValue(pointList);
		}

		// 3回合数字高亮点
		Point ps21 = new Point(900, 66);// 颜色：243;243;243 Color c = new Color(243, 243, 243);
		Point ps22 = new Point(903, 74);// 颜色：221;221;221 Color c = new Color(221, 221, 221);
		Point ps23 = new Point(907, 79);// 颜色：226;226;226 Color c = new Color(226, 226, 226);
		Point ps24 = new Point(897, 82);// 颜色：255;255;255 Color c = new Color(255, 255, 255);

		pointList = new ArrayList<>();
		pointList.add(ps21);
		pointList.add(ps22);
		pointList.add(ps23);
		pointList.add(ps24);
		// 第三回合
		// 等待
		CommonMethods.waitToAttack(null,LOGGER);
		LOGGER.info("第三回合开始：");
		eveValue = GameUtil.getColorEveValue(pointList);
		while (eveValue > THRESHOLD && battleRounds < MaxRounds) {
			battleRounds++;
			/*
			  连续使用技能组
			 */
			giveServantSkills(EventFactors.getExpPreSkills(DEFAULT_SKILL_COLORS));
			// 宝具平A
			pc = CommonMethods.waitToAttack("3",LOGGER);
			if ("attack".equals(pc.getName())) {
				CommonMethods.attackBAAForEvent(true, hases, LOGGER);
			}else {
				LOGGER.info("羁绊了，跳出来");
				break;
			}
			// 等待
			CommonMethods.waitToAttack("3", LOGGER);
			eveValue = GameUtil.getColorEveValue(pointList);
		}
	
	}
}
